package com.nit.lab.service.medical.impl;

import com.nit.lab.dto.medical.PatientDTO;
import com.nit.lab.dto.utils.PagingDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.entity.medical.PatientEntity;
import com.nit.lab.mapper.medical.PatientMapper;
import com.nit.lab.service.medical.PatientService;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
@Service
public class PatientServiceImpl implements PatientService {

    private final PatientMapper PatientMapper;

    @Autowired(required = false)
    public PatientServiceImpl(PatientMapper PatientMapper) {
        this.PatientMapper = PatientMapper;
    }


    @Override
    public TableRspDTO list4Table(TableReqDTO tableReqDTO) throws Exception {
        Long count = PatientMapper.count4Table(
                tableReqDTO.parseQueryParam("patientName"),tableReqDTO.parseQueryParam("patientCard"));
        System.out.print(tableReqDTO.parseQueryParam("patientName"));
        PagingDTO pagingDTO = new PagingDTO(
                tableReqDTO.getPageNo(),
                tableReqDTO.getPageSize(),
                count,
                PatientMapper.list4Table(
                        tableReqDTO.getStart(),
                        tableReqDTO.getPageSize(),
                        tableReqDTO.parseQueryParam("patientName"),
                        tableReqDTO.parseQueryParam("patientCard"),
                        tableReqDTO.getSortField(),
                        tableReqDTO.getSortOrder()));
        TableRspDTO tableRspDTO = new TableRspDTO(pagingDTO);
        return tableRspDTO;
    }

    @Override
    public Integer add(PatientDTO PatientDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        PatientEntity PatientEntity = mapperFactory.getMapperFacade().map(PatientDTO, PatientEntity.class);
        //PatientEntity.setGmtCreate(new Date());
        return PatientMapper.add(PatientEntity);
    }

    @Override
    public Integer update(PatientDTO PatientDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        PatientEntity PatientEntity = mapperFactory.getMapperFacade().map(PatientDTO, PatientEntity.class);
        return PatientMapper.update(PatientEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer remove(String PatientIds) throws Exception {
        String[] arrayPatientId = PatientIds.split(",");
        for (int i = 0; i < arrayPatientId.length; i++) {
            System.out.println(arrayPatientId[i]);
            PatientMapper.remove(Long.parseLong(arrayPatientId[i]));
        }
        return arrayPatientId.length;
    }


    @Override
    public PatientDTO getPatientById(Long PatientId) throws Exception {
        return PatientMapper.getPatientById(PatientId);
    }
}
